﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CoreData;

namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class frmSelectClassFilter : DevExpress.XtraEditors.XtraForm
    {
        public frmSelectClassFilter()
        {
            InitializeComponent();
            LoadData();
        }

        public long ID { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }

        void LoadData()
        {
            string _searchStr = " Class.IsDeleted = 0 and";
            if (!string.IsNullOrEmpty(txtCode.Text))
                _searchStr += " Class.Code like '%"+txtCode.Text + "%' and ";
            if (BranchiD > 0)
                _searchStr += " Class.BranchID=" + BranchiD.ToString() + " or";
            if (CourseiD > 0)
                _searchStr += " Class.CourseID= " + CourseiD.ToString() + " or";
            if (_searchStr.Length > 3)
           _searchStr= _searchStr.Substring(0, _searchStr.Length - 3);

            var model = RegisterService.RegisterServices.ClassService;
            var data = (List<Class>)model.GetList(_searchStr);

            if (data == null)
            {
                data = new List<Class>();
            }
            else
            {
                data = data.Where(c => c.Active == false).OrderByDescending(c => c.CreateDate).ToList();
            }

            gridControl1.DataSource = data;
            gridControl1.RefreshDataSource();
            gridView1.ViewCaption = "Danh sách có " + gridView1.RowCount + " dòng";

            PropertyInfo[] propertyInfos;
            propertyInfos = new Class().GetType().GetProperties(); ;

            foreach (PropertyInfo proInfo in propertyInfos)
            {
                AttributeCollection attributes = TypeDescriptor.GetProperties(new Class().GetType())[proInfo.Name].Attributes;

                DescriptionAttribute myAttribute =
                   (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
                if (String.IsNullOrEmpty(myAttribute.Description))
                {
                    gridView1.Columns[proInfo.Name].Visible = false;
                }
                else
                {
                    gridView1.Columns[proInfo.Name].Caption = myAttribute.Description;
                    gridView1.Columns[proInfo.Name].OptionsColumn.AllowEdit = false;
                }
            }

            gridView1.Columns["BranchExt"].GroupIndex = 0;
            gridView1.Columns["CourseExt"].GroupIndex = 1;
            gridView1.ExpandAllGroups();

        }

        int FocusedRow
        {
            get
            {
                return gridView1.FocusedRowHandle;
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.RowCount != 0)
            {
                ID = long.Parse(gridView1.GetRowCellValue(FocusedRow, "ID").ToString());
                Name = (gridView1.GetRowCellValue(FocusedRow, "Name").ToString());
                Code = (gridView1.GetRowCellValue(FocusedRow, "Code").ToString());
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }
        long CourseiD;
        long BranchiD;

        private void buttonEdit_Branch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectBranch select = new frmSelectBranch();
                if (select.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + select.Code + "] " + select.Name;
                    buttonEdit_Branch.EditValue = data;
                    BranchiD = select.ID;
                    if (BranchiD > 0)
                    {
                        buttonEdit_Course.Enabled = true;
                    }
                }
                else
                {
                    buttonEdit_Branch.EditValue = ""; BranchiD = 0;
                }
            }
        }

        private void buttonEdit_Course_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectCourse select = new frmSelectCourse();
                if (select.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + select.Code + "] " + select.Name;
                    buttonEdit_Course.EditValue = data;
                    CourseiD = select.ID;
                }
                else
                {
                    buttonEdit_Course.EditValue = ""; CourseiD = 0;
                }
            }
        }

        private void cmdtim_Click(object sender, EventArgs e)
        {
            LoadData();
        }



    }
}
